// 商品分类模块
import { findHeadCategory } from '@/api/cate.js'
import { category } from '@/utils/constant.js'

export default {
  namespaced: true,
  state: {
    // list: [{id: 0, name: '居家'}, {id: 1, name: '美食'}]
    list: category.map((item, index) => ({ id: index, name: item }))
  },
  mutations: {
    initList (state, payload) {
      state.list = payload
    }
  },
  actions: {
    async loadCateList (context) {
      const ret = await findHeadCategory()
      if (ret.result) {
        context.commit('initList', ret.result)
      }
    }
  },
  getters: {
    cateList (state) {
      return state.list.map(item => {
        // 保留两个二级分类
        // item.children = item.children.filter((cate, index) => index < 2)
        // return item
        return {
          ...item,
          children: item.children && item.children.filter((cate, index) => index < 2)
        }
      })
    }
  }
}
